//
//  TestYRPlayerSliderViewController.swift
//  YRProject
//
//  Created by 薛永伟 on 2019/9/26.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit

class TestYRPlayerSliderViewController: UIViewController,StoryboardLoadable {

    lazy var timer: YRTimer = {
        let timer = YRTimer.init(interval: 1.0, repeats: true, repeatRun: {
            self.updateSlider()
        })
        return timer
    }()
    
    var current:Int = 1
    
    lazy var slider: YRPlayerSlider = {
        let view = YRPlayerSlider.init()
        view.minimumValueImage = UIImage.init(named: "min")
        view.maximumValueImage = UIImage.init(named: "max")
        let img = UIImage.init(color: UIColor.white, size: CGSize.init(width: 64.scaleLength, height: 14.scaleLength))!
        let rltImg = img.byRoundCornerRadius(7.scaleLength)
        view.setThumbImage(rltImg, for: .normal)
        view.valueTextColor = UIColor.init(rgb: 0x5A5F63)
        view.valueTextFont = UIFont.systemFont(ofSize: 10, weight: .regular)
        view.isContinuous = false
        view.addTarget(self, action: #selector(sliderChanged(slider:)), for: .valueChanged)
        return view
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.init(rgb: 0x37333C)
        
        view.addSubview(slider)
        slider.translatesAutoresizingMaskIntoConstraints = false
        slider.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 30).isActive = true
        slider.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30).isActive = true
        slider.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30).isActive = true
        slider.heightAnchor.constraint(equalToConstant: 20).isActive = true
        
        self.timer.start()
        
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        
        super.viewDidDisappear(animated)
        self.timer.close()
    }
    
    @objc func sliderChanged(slider:UISlider){
        
        let value = Int(slider.value + 0.5)
        current = value
    }
    
    func updateSlider(){
        
        current += 1
        
        self.slider.updateSeconds(current, total: 100)
    }
    

}
